import BasicController from '../BasicController';
import { handleString } from '../../utils/utils';
const moment = require('moment');
const assert = require('assert');

module.exports = class Sign extends BasicController {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }
  async usersignAction() {
    const { user_id, systemString, networkString } = this.post();
    const systemInfo = handleString(systemString);
    const networkInfo = handleString(networkString);
    const search = {
      data: {
        sign_time: [
          'BETWEEN',
          moment(new Date(new Date().toLocaleDateString())).format('YYYY-MM-DD HH:mm:ss'),
          moment(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).format('YYYY-MM-DD HH:mm:ss')
        ]
      },
      model: 'sign'
    };
    const signList = await this.model(this.MODEL).signListAction(search);
    if (signList.length > 0) {
      return this.fail(1, '今天已经签到过了', null);
    }
    const params = {
      data: {
        id: think.uuid('v4'),
        user_id,
        sign_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        create_time: moment().format('YYYY-MM-DD HH:mm:ss')
      },
      model: 'sign'
    };
    const result = await this.model(this.MODEL).usersignAction(params);
    return this.success(result, '签到成功');
  }
  async countAction() {
    const { user_id } = this.post();
    const params = {
      data: {
        user_id
      },
      model: 'sign'
    };
    const result = await this.model(this.MODEL).countAction(params);
    return this.success(result);
  }
};
